#include "str.h"
#include <stdlib.h>

int skip_space(char *str)
{
    char *p = str;
    while((*p != '\0') && (*p == ' ' || *p == '\t' || *p == '\n'))
        p++;

    return p - str;
}

int str_to_int(const char *ptr)
{
    int val = 0;

    while(*ptr >= '0' && *ptr <= '9')
    {
        val = val * 10 + (*ptr - '0');
        ptr++;
    }

    return val;
}

int simple_cmp(const char *str, const char *inst)
{
    int left = strlen(str);
    int n = strlen(inst);

    if (left < n)
        return 0;

    if (strncmp(str, inst, n) == 0)
        return n;
    
    return 0;
}

int is_alpha(char c)
{
    if ((c >= '0' && c <= '9')
        || (c >= 'a' && c <= 'z')
        || (c >= 'A' && c <= 'Z'))
    {
        return 1;
    }

    return 0;
}

int get_word(const char *src, char *dst)
{
    char *cp = dst;
    const char *ptr = src;
    
    while(*ptr != '\0' && is_alpha(*ptr))
        *cp++ = *ptr++;

    return ptr - src;
}

char *strdump(const char *str, int len)
{
    char *ret = malloc(len + 1);
    memcpy(ret, str, len);
    ret[len] = 0;
    return ret;
}

